package com.itheima.hello.UDP通信;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception {
        //创建客户端对象(Client)发送数据包
        DatagramSocket socket = new DatagramSocket();
        //  public DatagramPacket(byte buf[], int length,
        //                          InetAddress address, int port) {
        //        this(buf, 0, length, address, port);
        //    }
        //参数一：发送出去的数据
        //参数二：发送数据包的长度
        //参数三：服务端发送IP地址（找到服务端主机）
        //参数四：服务端程序的端口号
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入(输入exit即可退出):");
            String s = sc.nextLine();
            if ("exit".equals(s)){
                System.out.println("已经成功退出");
                socket.close();
                break;
            }
            byte[] bytes = s.getBytes();
            //数据包
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(),8888);
            //发送数据
            socket.send(packet);
        }
        //关闭客户端
//        socket.close();
//        System.out.println("客户端数据发送完毕");
    }
}
